﻿using System;
using System.Collections.ObjectModel;
using System.Web.Security;
using System.Web.UI;
using Microsoft.Practices.Web.UI.WebControls;
using Ornament.MemberShip;
using Ornament.MemberShip.Module.Controllers;
using Ornament.MemberShip.Module.Presenters;
using Ornament.MemberShip.Module.Views;
using Ornament.Util;
using Ornament.Web.Util;

public partial class Admin_MemberAdmin_Controls_UserEditorViewCtl : UserControl, IUserEditView
{
    #region IUserEditView Members

    /// <summary>
    /// 获取当前被编辑的登陆名
    /// </summary>
    public string LoginId
    {
        get
        {
            string result = Request.QueryString["loginId"];
            if (!String.IsNullOrEmpty(result))
                return result;
            return null;
        }
    }

    /// <summary>
    /// 获取是否处于编辑
    /// </summary>
    public bool IsEditing
    {
        get { return LoginId != null; }
    }

    public IRoleAssignView RoleAssignView
    {
        get { return RoleAssign1; }
    }

    public void BindRoles(ReadOnlyCollection<Role> roles)
    {
        GridViewRole.DataSource = roles;
        GridViewRole.DataBind();
    }

    public IUserGroupAssignView UserGroupAssignView
    {
        get { return UserGroupAssignView1; }
    }

    public event EventHandler<DataEvent<User>> DeletdeEvent;

    public UserEditPresenter Presenter { set; get; }

    public void BindToView(User user)
    {
        ObjectContainerDataSource1.DataSource = user;
    }

    #endregion

    protected void Page_Load(object sender, EventArgs e)
    {
        Presenter = new UserEditPresenter(this);

        if (!IsPostBack)
        {
            Presenter.InitilizeView();
            ChangePassword1.UserName = LoginId;
        }
        Presenter.ViewLoad();
    }

    protected void GetUser(object sender, ObjectContainerDataSourceSelectingEventArgs e)
    {
        Presenter.BindUser();
    }

    protected void OnCreatingFromPresenter(object sender, ObjectContainerDataSourceCreatingObjectArgs e)
    {
        User u = Presenter.GetUser();
        e.NewObjectInstance = u;
    }

    protected void onUpdateUser(object sender, ObjectContainerDataSourceStatusEventArgs e)
    {
        Presenter.SaveChange((User) e.Instance);
    }

    protected void onDeleteUser(object sender, ObjectContainerDataSourceStatusEventArgs e)
    {
        Presenter.Delete((User) e.Instance);
        if (DeletdeEvent != null)
        {
            DeletdeEvent(this, new DataEvent<User>((User) e.Instance));
        }
    }

    protected void ChangePassword1_ContinueButtonClick(object sender, EventArgs e)
    {
        Page.Eval(string.Format("location=\"{0}\"", Request.RawUrl));
    }

    protected void SendPassword(object sender, EventArgs e)
    {
    }

    protected void Save(object sender, EventArgs e)
    {
        FormView1.UpdateItem(true);
    }

    protected void ResetPassword(object sender, EventArgs e)
    {
        string password = Membership.GeneratePassword(Membership.MinRequiredPasswordLength,Membership.MinRequiredNonAlphanumericCharacters);

    }
}